<!-- subpkg_consult/patient/index.vue -->
<script setup>
import { computed, ref } from 'vue'
import { getPatientListAPI,delPatientAPI } from '@/apis/patient.js'
import {onShow} from '@dcloudio/uni-app'
 // 侧滑按钮配置
const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d'
    }
  }
])
//保存患者列表
const patientList = ref([])
//获取患者列表数据
const getPatientList = async() => {
	const res = await getPatientListAPI()
	console.log(res)
	patientList.value = res.data
}
//删除患者
const onDelPatient = (id) => {
	// console.log(id)
	uni.showModal({
		content:'确定要删除吗?',
		success:async(res)=>{
			if(res.confirm){
				await delPatientAPI(id)
				uni.showToast({
					title:'删除成功',
				})
				getPatientList()
			}
		}
	})
}
// 患者卡片索引值
const patientCardIndex = ref(0)
//选择患者
const onChoosePaitent = (index) => {
	patientCardIndex.value = index
}

//选择患者的id
const patientId  = computed(()=>{
	return patientList.value[patientCardIndex.value].id
})
onShow(()=>{
	getPatientList()
})
</script>
<template>
  <scroll-page>
    <view class="patient-page">
      <view class="page-header">
        <view class="patient-title"> 请选择患者信息 </view>
        <view class="patient-tips">
          以便医生给出更准确的治疗，信息仅医生可见
        </view>
      </view>
      <uni-swipe-action>
        <uni-swipe-action-item :right-options="swipeOptions" v-for="(item,index) in patientList" :key="item.id" @click="onDelPatient(item.id)">
          <view class="archive-card" @click="onChoosePaitent(index)" :class="{active:patientCardIndex===index}">
            <view class="archive-info">
              <text class="name">{{item.name}}</text>
              <text class="id-card">{{item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')}}</text>
              <text class="default" v-if='item.defaultFlag  === 1'>默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{item.genderValue}}</text>
              <text class="age">{{item.age}}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="true" class="archive-card" v-show="patientList.length < 6">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpkg_archive/form/index"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label" >添加患者</text>
        </navigator>
      </view>
    </view>
    <!-- 下一步操作 -->
    <view class="next-step">
      <button class="uni-button" url="/subpkg_consult/payment/index">下一步</button>
    </view>
  </scroll-page>
</template>

<style lang="scss">
@import './index.scss';
</style>